package cn.ffcs.uec.payroll.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.payroll.component.bean.UecPayRollboxExtBean;
import cn.ffcs.uec.payroll.model.UecPayRoll;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import lombok.Getter;
import lombok.Setter;
@Controller
@Scope("prototype")
public class UecPayRollboxExt extends Div implements IdSpace {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6487871788980086081L;
	/**
	 * 对应页面上的bean
	 */
	private UecPayRollboxExtBean bean = new UecPayRollboxExtBean();
	/**
	 * 查询实体
	 */
	private UecPayRoll qryUecPayRoll;
	
	/**
	 * 实体
	 */
	private UecPayRoll uecPayRoll;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPayRollboxExt() {
		Executions.createComponents("/pages_nmg/uec_payroll/comp/uec_payroll_box_ext.zul",
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecStaffConstants.ON_UEC_PARTYLOG_QUERY, this,
				UecStaffConstants.ON_UEC_PARTYLOG_QUERY_RESPONSE);

		this.addForward(UecStaffConstants.ON_CLEAN_UEC_PARTYLOG, this,
				UecStaffConstants.ON_CLEAN_UEC_PARTYLOG_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		List<NodeVo> nodeVoList = new ArrayList<NodeVo>();
		NodeVo nodeVo = new NodeVo();
		nodeVo.setName("申请单名称");
		nodeVo.setId("1");
		
		NodeVo nodeVo2 = new NodeVo();
		nodeVo2.setName("申请单编码");
		nodeVo2.setId("2");
		
		
		nodeVoList.add(nodeVo);
		nodeVoList.add(nodeVo2);
		
		ListboxUtils.rendererForEdit(bean.getCondition(), nodeVoList);
		setButtonValid(true,false,false);
		PubUtil.disableComponent(this.bean.getUecPayRollEditExt(),true);
		
	}


	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecPartyLogResponse(ForwardEvent event) {
		clearVariable();
		uecPayRoll = null;
		qryUecPayRoll = null;
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid( boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecPayRoll() {
		//this.bean.getUecPartyLogListboxPaging().setActivePage(0);
		clearVariable();
		
		//PubUtil.clearComponent(this.bean.getBasePanel());
		if (!StrUtil.isNullOrEmpty(bean.getCondition().getSelectedItem().getValue()) 
				&& !StrUtil.isNullOrEmpty(bean.getConditionValue().getValue())) {
			Listbox listbox = bean.getCondition();
			String listboxStr = (String) listbox.getSelectedItem().getValue();
			int listboxId = Integer.parseInt(listboxStr);
			qryUecPayRoll = UecPayRoll.newInstance();
			switch (listboxId) {
			case 1:
				qryUecPayRoll.setBillname(bean.getConditionValue().getValue());
				break;
			case 2:
				qryUecPayRoll.setBillcode(bean.getConditionValue().getValue());
				break;
			default:
				break;
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("qryUecPayRoll", qryUecPayRoll);
		Window window = (Window) Executions.createComponents(
				"/pages_nmg/uec_payroll/comp/uec_payroll_list.zul", this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						setButtonValid(true,true,true);
						uecPayRoll = (UecPayRoll) event.getData();
						if (!StrUtil.isEmpty(uecPayRoll.getPkPayroll())) {
							setButtonValid(true,false,false);
						}
						PubUtil.fillBeanFromPo(uecPayRoll, bean.getUecPayRollEditExt().getBean());
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecPayRoll() {
		clearVariable();
		bean.getConditionValue().setValue(null);
		PubUtil.clearComponent(this);
		
	}
	
	/**
	 * 
	 * @param opType
	 */
	private void openUecEditWin(String opType) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		if ("view".equals(opType) || "edit".equals(opType)) {
			if (null != uecPayRoll) {
				map.put("uecPayRoll", uecPayRoll);
			} else {
				ZkUtil.showError("请选择一条记录！", "提示信息");
				return;
			}
		} 
		Window window = (Window) Executions.createComponents(
				"/pages_nmg/uec_payroll/comp/uec_payroll_edit.zul", this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						setButtonValid(true,true,true);
						uecPayRoll = (UecPayRoll) event.getData();
						PubUtil.fillBeanFromPo(uecPayRoll, bean.getUecPayRollEditExt().getBean());
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecPayRoll() throws Exception {
		openUecEditWin("add");
	}
	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecPayRoll() throws Exception, InterruptedException {
		openUecEditWin("edit");
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecPayRoll() {
		if (this.uecPayRoll != null ) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						//uecPartyLog.setId(id);
						uecPayRoll.removeOnly();
						PubUtil.clearComponent(bean.getUecPayRollEditExt());
						uecPayRoll = null;
						Messagebox.show("删除成功！");
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}


	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPayRoll = null;
		qryUecPayRoll = null;
	}


}
